/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-27 20:36:15
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-16 17:38:32
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BopDescEditDto, StoreServiceProxy, StoreContentDto, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';

@Component({
    selector: 'app-add-or-eddit',
    templateUrl: './add-or-eddit.component.html',
    styleUrls: ['./add-or-eddit.component.less']
})
export class AddOrEdditComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //要修改的数据
    @Input() title: string;  //标题
    workingHourOption = [
        {
            label: '请选择',
            value: undefined
        }, {
            label: '工步',
            value: '1'
        }, {
            label: '工序',
            value: '0'
        }
    ];
    saving = false;
    active = false;
    stroeTyp = { label: undefined, value: undefined };
    isOpen = true;
    stroeTypeNameOptions = [];
    form: StoreContentDto = new StoreContentDto();
    saveFlag = true;
    constructor(
        injector: Injector,
        private _storeServiceProxy: StoreServiceProxy,
        private _messageService: MessageService,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    //保存BOP任务
    save(): void {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
            //获取实体
            let input = new StoreContentDto();
            input.stroeName = this.form.stroeName;
            input.stroeTypeId = this.stroeTyp.value;
            input.stroeTypeName = this.stroeTyp.label;
            input.remark = this.form.remark;
            if (this.title == '仓库修改') {
                input.id = this.form.id;
            }
            if (this.isOpen) {
                input.storeUse = 1;
            } else {
                input.storeUse = 0;
            }
            this.saving = true;
            this._storeServiceProxy.addOrUpdate(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(() => {
                    this._messageService.success('保存成功');
                    this.close();
                    this.modalSave.emit(null);
                });
        // }
    }
    //打开弹窗
    onShown(): void {
        this._storeServiceProxy.getStoreTypeList(
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.stroeTypeNameOptions = result;
            });
    }
    //查看新增任务
    show(): void {
        if (this.title == '仓库修改') {
            this.initEditInfo()
        } else {
            this.form = new StoreContentDto();
        }
        this.active = true;
        this.modal.show();

    }
    //初始化修改数据
    initEditInfo() {
        this.form = this.editData;
        this.stroeTyp.value = this.editData.stroeTypeId;
        this.stroeTyp.label = this.editData.stroeTypeName;
        if (this.editData.storeUse == 1) {
            this.isOpen = true;
        } else {
            this.isOpen = false;
        }
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.saveFlag = true;
        this.form = new StoreContentDto();
        this.stroeTyp.value = undefined;
        this.stroeTyp.label = undefined;
        this.active = false;
        this.modal.hide();
    }


}
